/**
 * Created by LuBangTao on 2017/3/27.
 */
var fireworkVersion8 = function (scene) {
    $(".cut-show").css("max-width", $(window).width());
    $(".cut-show").css("max-height", $(window).height());
    var myPoints = [
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(screenLength, 0, 0),
        new BABYLON.Vector3(screenLength, 0, screenWidth),
        new BABYLON.Vector3(0, 0, screenWidth),
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(0, screenHeight, 0),
        new BABYLON.Vector3(screenLength, screenHeight, 0),
        new BABYLON.Vector3(screenLength, screenHeight, screenWidth),
        new BABYLON.Vector3(0, screenHeight, screenWidth),
        new BABYLON.Vector3(0, screenHeight, 0),
        new BABYLON.Vector3(0, 0, 0),
        new BABYLON.Vector3(0, 0, screenWidth),
        new BABYLON.Vector3(0, screenHeight, screenWidth),
        new BABYLON.Vector3(screenLength, screenHeight, screenWidth),
        new BABYLON.Vector3(screenLength, 0, screenWidth),
        new BABYLON.Vector3(screenLength, 0, 0),
        new BABYLON.Vector3(screenLength, screenHeight, 0),
    ];
    secondCanvas.width = screenLength * screenWidth;
    secondCanvas.height = screenHeight;
    currentFireCount++;
    var thisFireCount = currentFireCount;
    var lines = BABYLON.MeshBuilder.CreateLines(thisFireCount + "lines", {points: myPoints, updatable: true}, scene);
    if (countCapacity < maxCapacity) {
        countCapacity++;
    } else {
        Lighting.sendMessageToServer(0);
    }
    var startSphere = new BABYLON.Mesh.CreateSphere(thisFireCount + "Shoot", 4, 1, scene);
    startSphere.position.x = screenLength / 2;
    startSphere.position.z = screenWidth / 2;
    startSphere.isVisible = false;
    var particleSystem = new BABYLON.ParticleSystem(thisFireCount + "particles", 20, scene);
    particleSystem.particleTexture = new BABYLON.Texture("texture/flare.png", scene);
    particleSystem.emitter = startSphere; // the starting object, the emitter
    particleSystem.minEmitBox = new BABYLON.Vector3(0, 0, 0); // Starting all from
    particleSystem.maxEmitBox = new BABYLON.Vector3(0, 0, 0); // To...
    particleSystem.color1 = new BABYLON.Color4(.5, .5, .5, 1.0);
    particleSystem.color2 = new BABYLON.Color4(.5, .5, .5, 1.0);
    particleSystem.colorDead = new BABYLON.Color4(0, 0, 0, 0);
    particleSystem.minSize = 1.0;
    particleSystem.maxSize = 1.0;
    particleSystem.minLifeTime = .25;
    particleSystem.maxLifeTime = .25;
    particleSystem.emitRate = 50;
    particleSystem.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
    particleSystem.minEmitPower = 1;
    particleSystem.maxEmitPower = 1;
    particleSystem.updateSpeed = 0.005;
    var bigEnough = false;
    var updateFunction = function (particles) {
        for (var index = 0; index < particles.length; index++) {
            var particle = particles[index];
            particle.age += this._scaledUpdateSpeed;
            if (particle.age >= particle.lifeTime) {
                this.recycleParticle(particle);
                index--;
                continue;
            }
            else {
                if (!bigEnough) {
                    particle.size -= .02;
                }
                Lighting.drawFireworkData(particle.position.x, particle.position.y, particle.position.z, screenHeight, screenLength, secondCanvas.getContext("2d"),particle.color)
                particle.direction.scaleToRef(particleSystem._scaledUpdateSpeed, particleSystem._scaledDirection);
                particle.position.addInPlace(particleSystem._scaledDirection);
                particleSystem.gravity.scaleToRef(particleSystem._scaledUpdateSpeed, particleSystem._scaledGravity);
                particle.direction.addInPlace(particleSystem._scaledGravity);
            }
        }
    };
    particleSystem.updateFunction = updateFunction;
    particleSystem.start();
    var isTop2 = false;
    var isFired2 = false;
    scene.registerBeforeRender(function () {
        secondCanvas.width = secondCanvas.width;
        if (!isFired2) {
            if (!isTop2) {
                startSphere.position.y += .1;
                if (startSphere.position.y > screenHeight * 2 / 3) {
                    isTop2 = !isTop2;
                    particleSystem.stop();
                }
            } else {
                setTimeout(function () {
                    fire(scene);
                }, 1000);
                isFired2 = !isFired2;
            }
        }
    });
    function fire(scene) {
        var particleSystem1 = new BABYLON.ParticleSystem(thisFireCount + Lighting.getRandomString(6), screenHeight * 200, scene);
        var startPositionFunction = function (worldMatrix, positionToUpdate) {
            var r = 1;
            var v3 = getCart(this.domeRadius);
            BABYLON.Vector3.TransformCoordinatesFromFloatsToRef(v3.x * r, v3.y * r, v3.z * r, worldMatrix, positionToUpdate);
        };
        var updateFunction = function (particles) {
            for (var index = 0; index < particles.length; index++) {
                var particle = particles[index];
                particle.age += this._scaledUpdateSpeed;
                if (particle.age >= particle.lifeTime) {
                    this.recycleParticle(particle);
                    index--;
                    continue;
                }
                else {
                    particle.color = new BABYLON.Color4(Math.random(), Math.random(), Math.random() , 1);
                    Lighting.drawFireworkData(particle.position.x, particle.position.y, particle.position.z,screenHeight, screenLength, secondCanvas.getContext("2d"),particle.color);
                    particle.direction.scaleToRef(particleSystem1._scaledUpdateSpeed, particleSystem1._scaledDirection);
                    particle.position.addInPlace(particleSystem1._scaledDirection);
                    particleSystem1.gravity.scaleToRef(particleSystem1._scaledUpdateSpeed, particleSystem1._scaledGravity);
                    particle.direction.addInPlace(particleSystem1._scaledGravity);
                }
            }
        };
        particleSystem1.updateFunction = updateFunction;
        particleSystem1.startPositionFunction = startPositionFunction;
        particleSystem1.domeRadius = .5;
        particleSystem1.particleTexture = new BABYLON.Texture("texture/flare.png", scene);
        particleSystem1.emitter =new BABYLON.Vector3(screenLength / 2, screenHeight / 2, screenWidth / 2);
        particleSystem1.minEmitBox = new BABYLON.Vector3(0, 0, 0);
        particleSystem1.maxEmitBox = new BABYLON.Vector3(0, 0, 0);
        particleSystem1.color1 = new BABYLON.Color4(Math.random() * 2, Math.random() * 2, Math.random() * 2, 1);
        particleSystem1.color2 = particleSystem1.color1;
        particleSystem1.colorDead = new BABYLON.Color4(0, 0, 0, 0.0);
        particleSystem1.minSize = .5;
        particleSystem1.maxSize = .5;
        particleSystem1.minLifeTime = 1;
        particleSystem1.maxLifeTime = 1;
        particleSystem1.emitRate = 4000;
        particleSystem1.blendMode = BABYLON.ParticleSystem.BLENDMODE_ONEONE;
        particleSystem1.minEmitPower = 0;
        particleSystem1.maxEmitPower = 0;
        particleSystem1.updateSpeed = 0.005;
        var timer1 = 0;
        particleSystem1.start();
        var isDone = false;
        var isDispose = false;

        function getCart(radius) {
            //球心（A,B,C）：(5,20,5)
            //球方程：(X-A)^2+(Y-B)^2+(Z-C)^2=r^2;
            var r = minValue/2;
            var x = Math.random() * screenWidth+(-1)*Math.random() * screenWidth;
            var y = Math.random() * screenHeight +(-1)*Math.random() * screenHeight;
            var z = Math.random() * screenLength+(-1)*Math.random() * screenLength;
            // if (Math.pow((x - screenWidth/2), 2) + Math.pow((y - screenHeight/2), 2) + Math.pow((z - screenLength/2), 2) < Math.pow(r, 2)) {
            if (Math.pow((x), 2) + Math.pow(y-screenHeight/2+minValue/2,2 ) + Math.pow((z), 2) < Math.pow(r, 2)) {
                return new BABYLON.Vector3(x, y, z);
            } else {
                return getCart(radius);
            }
        }
        scene.registerBeforeRender(function () {
            if (!isDone) {
                if (timer1 < screenHeight / 3) {
                    timer1 += 0.1
                } else {
                    particleSystem1.stop();
                    isDone = !isDone;
                }
            } else {
                if (!isDispose) {
                    timer1 += 0.1;
                    if (timer1 > screenHeight) {
                        startSphere.dispose();
                        lines.dispose();
                        for (var i = 6; i < scene.meshes.length; i++) {
                            if (scene.meshes[i].id.indexOf(thisFireCount.toString()) >= 0) {
                                scene.meshes[i].dispose();
                            }
                        }
                        countCapacity--;
                        isDispose = !isDispose;
                        timer1 = 0;
                    }

                }
            }
        });
    }
};
